from django import http
from django.contrib.auth import logout
from django.shortcuts import render, redirect
from django.views.generic import View
from meiduo_mall.utils.my_category import get_categories
from contents.models import ContentCategory
from goods.models import GoodsChannel


class IndexView(View):

    def get(self, request):
        categories = get_categories()

        # 获取广告内容
        contents = {}
        content_categorys = ContentCategory.objects.all()
        for content_category in content_categorys:
            contents[content_category.key] = content_category.content_set.all()

        content = {
            'categories': categories,
            'contents': contents
        }
        return render(request, 'index.html', context=content)


class LogOutView(View):

    def get(self, request):
        # 清除session
        logout(request)
        # 删除cookie
        rsp = redirect('/')
        rsp.delete_cookie('username')
        return rsp
